const Biz = require('../biz/index');
const DB = require('../db/index');
const fw = require('../core/framework');
const exception = require('../exception');

module.exports = {
    init(router,urlencodedParser){
        // todo 应该是商户登录 还是用户登录？
        router.post('/merchant/login', urlencodedParser, function (req, res) {
            let param = fw.utils.parseBody(req);
            let cData = {
                mobile:param.account,
                password:param.password,
            };
            Biz.User.login(cData,function(data){
                let loginRes = JSON.parse(JSON.stringify(data));
                let token = fw.utils.getGuid();
                loginRes.data['token'] = token;
                fw.config.loginUsers[token] = {
                    userId:loginRes.data.id,
                    merchantId:loginRes.data.merchantId
                };
                delete loginRes.data.password;
                // loginUsers[token] = data.data.id; // 记录用户id和token一对一。
                res.send(loginRes);
            },function(data){
                res.send(data);
            });
        });
        router.post('/user/create', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                let userData = {
                    username:param.username,
                    mobile:param.mobile,
                    password:param.password,
                    role:param.role || 2,
                    wxcode: param.wxcode || null,
                    head_image:param.head_image || null,
                    merchant_id:fw.parseMerchantId(req)
                };
                Biz.User.create(userData,(userDBRes)=>{
                    console.log('userDBRes++++>>>>>>>>>>>>>>>>>>',userDBRes)
                    let alarmContactGroupUserData = {
                        alarm_contact_group_id:param.alarm_contact_group_id,
                        user_id:userDBRes.data.id,
                        merchant_id:fw.parseMerchantId(req)
                    };
                    // 添加用户到报警联系人和用户关系表
                    Biz.AlarmContactGroupUser.create(alarmContactGroupUserData,(alarmContactGroupUserDBRes)=>{
                        res.send(userDBRes);
                    },(alarmContactGroupUserDBRes)=>{
                        res.send(alarmContactGroupUserDBRes);
                    });
                },(userDBRes)=>{
                    res.send(userDBRes);
                });
            })
        });
        router.post('/user/update', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                Biz.User.update(param,(data)=>{
                    DB.AlarmContactGroupUser.update({
                        alarm_contact_group_id:param.alarm_contact_group_id,
                        user_id:param.id
                    },'user_id').then(()=>{
                        res.send(data);
                    })

                },(data)=>{
                    res.send(data);
                });
            });
        });
        router.post('/user/delete', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                console.log('执行删除账户业务');
                let param = fw.utils.parseBody(req);
                Biz.User.delete(param,(data)=>{
                    DB.AlarmContactGroupUser.delete({user_id:param.id},'user_id').then(()=>{
                        res.send(data);
                    });
                },(data)=>{
                    res.send(data);
                });
            });
        });
        router.post('/user/search', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                param.merchant_id = fw.parseMerchantId(req);
                Biz.User.search(param,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
    }
};
